package com.duoketech.controller.web.member;

import com.duoketech.controller.BaseController;
import com.duoketech.controller.web.ICRUD;
import com.duoketech.exception.ValidateException;
import com.duoketech.model.member.Member;
import com.duoketech.service.web.member.MemberService;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Page;

public class MemberController extends BaseController implements ICRUD {

    @Override
    public void listAll() {
        int pageNum = getParaToInt("pageNum",1);
        int pageSize = getParaToInt("pageSize",1);
        Page<Member> result = (Page<Member>) MemberService.ME.pageAllRecord(getModel(Member.class),pageNum,pageSize,Kv.create());
        renderObject(result);
    }

    @Override
    public void queryById() {
        int id = getParaToInt("id",0);
        try {
            setAttr("article",MemberService.ME.queryBy(id,getModel(Member.class),Kv.create()));
            render("edit-"+viewModel()+".html");
            } catch (ValidateException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void doAdd() {
        try {
            MemberService.ME.doAdd(getModel(Member.class),Kv.create());
            renderSuccess("操作成功");
            } catch (ValidateException e) {
            renderError(e.getMessage());
        }

    }

    @Override
    public void doUpdate() {
        try {
            MemberService.ME.update(getModel(Member.class),Kv.create());
            renderSuccess("操作成功");
            } catch (ValidateException e) {
            renderError(e.getMessage());
        }
    }

    @Override
    public void doDelete() {
        int id = getParaToInt("id",0);
        try {
        MemberService.ME.deleteBy(id,getModel(Member.class),Kv.create());
        renderSuccess("操作成功");
        } catch (ValidateException e) {
        renderError(e.getMessage());
        }
    }


    public void resetPass(){
        render("reset-pass.html");
    }

    public void doResetPass(){
        try {
            String oldPass = getPara("oldPass","");
            String newPass = getPara("newPass","");
            String confirmPass = getPara("confirmPass","");
            MemberService.ME.doResetPass(getShopMemberId(),oldPass,newPass,confirmPass);
            renderSuccess("密码重置成功，下次登录请使用新密码登录哦！");
        } catch (ValidateException e) {
            renderError(e.getMessage());
        }
    }
    
    public void bindShopAccout(){
    	 render("bind-new-shop-accout.html");
    }
    
    public void doBindShopAccout(){
    	try {
            String member_ser = getPara("member_ser","");
            String mobile = getPara("mobile","");
            String password = getPara("password","");
            String confirmPass = getPara("confirmPass","");
            MemberService.ME.doBindShopAccout(mobile, password, confirmPass, member_ser);
            renderSuccess("绑定成功，请使用设定的手机号码和密码进行商品平台登录");
        } catch (ValidateException e) {
            renderError(e.getMessage());
        }
   }
  }